package com.example.whackit;

import java.io.IOException;
import com.example.whackit.R.id;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnTouchListener {

	Button vibrateBtn;
	Vibrator vibrator;

	final private MediaPlayer mediaPlayer = new MediaPlayer();

	private SensorManager mSensorManager;
	private Sensor mSensor;

	private float mAccel;
	private float mAccelCurrent;
	private float mAccelLast;
	private final SensorEventListener mSensorListener = new SensorEventListener() {
		public void onSensorChanged(SensorEvent se) {
			float x = se.values[0];
			float y = se.values[1];
			float z = se.values[2];

			mAccelLast = mAccelCurrent;
			mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
			float delta = mAccelCurrent - mAccelLast;
			mAccel = (float) Math.abs(mAccel * 0.9f + delta);

			if (mAccel > 11 && mAccel <= 15) {

				try {
					mediaPlayer.start();
					vibrator.vibrate(100);
					Thread.sleep(20);
					vibrator.vibrate(200);
					Thread.sleep(20);
					vibrator.vibrate(300);
					Thread.sleep(20);
					vibrator.vibrate(400);
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			TextView accelerationTextView = (TextView) findViewById(id.accelerationTextView);
			if (mAccel >= 9)
				accelerationTextView.setText("Current Acceleration is: "
						+ mAccel);
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		vibrateBtn = (Button) findViewById(id.vibrateBtn);
		vibrateBtn.setOnTouchListener(this);
		vibrator = (Vibrator) getSystemService(com.example.whackit.MainActivity.VIBRATOR_SERVICE);
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManager
				.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
		mSensorManager.registerListener(mSensorListener, mSensor, 200);
		mAccel = 0.00f;
		mAccelCurrent = SensorManager.GRAVITY_EARTH;
		mAccelLast = SensorManager.GRAVITY_EARTH;

		try {
			AssetFileDescriptor audio = getAssets().openFd(
					"audio/boobsAreGreat_Edited.mp3");
			mediaPlayer.setDataSource(audio.getFileDescriptor());
			mediaPlayer.prepare();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			vibrator.vibrate(5000);
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			vibrator.cancel();
		}
		return false;
	}
}
